﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<TanHungViet.Web.Models.Product>>" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="TanHungViet.Helper"%>


<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Giỏ Hàng - PHẦN MỀM XÂY DỰNG – TÂN HƯNG VIỆT – 0835.125.795
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript" src='<%=Url.Content("~/Scripts/json.js")%>'></script>
    <script type="text/javascript">
        function edit(id) {
            var idCount = "tdCount" + id;
            var count = $("#" + idCount).html().trim();
            $("#" + idCount).empty();
            var newHtml = "<input id='inpCount" + id + "' type='text' style='text-align:right;' size='7' value='" + count + "'/>";
            $("#" + idCount).html(newHtml);

            var idEdit = "btnEdit" + id;
            $("#" + idEdit).attr('value', 'Lưu');
            $("#" + idEdit).attr('onclick', 'save(' + id + ')');

            var idDelete = "btnDelete" + id;
            $("#" + idDelete).attr('value', 'Hủy');
            $("#" + idDelete).attr('onclick', 'cancel(' + id + ',' + count + ')');
        }

        function cancel(id, currentCount) {
            var idCount = "tdCount" + id;
            $("#" + idCount).empty();
            $("#" + idCount).html(currentCount);

            var idEdit = "btnEdit" + id;
            $("#" + idEdit).attr('value', 'Sửa');
            $("#" + idEdit).attr('onclick', 'edit(' + id + ')');

            var idDelete = "btnDelete" + id;
            $("#" + idDelete).attr('value', 'Xóa');
            $("#" + idDelete).attr('onclick', 'deleteProduct(' + id + ')');
        }

        function save(id) {
            var count = $("#inpCount" + id).val();
            var data = { productID: id, count : count };
            var jsonData = $.toJSON(data);

            $.ajax({
                url: '/CapNhatSoLuongSPGioHang',
                type: 'POST',
                dataType: 'json',
                data: jsonData,
                contentType: 'application/json; charset=utf-8',
                success: function(data) {
                    //                    var idCount = "tdCount" + id;
                    //                    $("#" + idCount).empty();
                    //                    $("#" + idCount).html(count);

                    //                    var idEdit = "btnEdit" + id;
                    //                    $("#" + idEdit).attr('value', 'Sửa');
                    //                    $("#" + idEdit).attr('onclick', 'edit(' + id + ')');

                    //                    var idDelete = "btnDelete" + id;
                    //                    $("#" + idDelete).attr('value', 'Xóa');
                    //                    $("#" + idDelete).attr('onclick', 'deleteProduct(' + id + ')');
                    alert(data.mess);
                    document.location = "/ChiTietGioHang";
                }
            });        
        }

        function deleteProduct(id) {
            var data = { productID: id };
            var jsonData = $.toJSON(data);

            $.ajax({
                url: '/XoaSanPhamGioHang',
                type: 'POST',
                dataType: 'json',
                data: jsonData,
                contentType: 'application/json; charset=utf-8',
                success: function(data) {
                    alert(data.mess);
                    document.location = "/ChiTietGioHang";
                }
            });
        }

        function submit() {
            document.location = "/HoanTatGioHang";
        }
    </script>

    <% CultureInfo ci = new CultureInfo(Session["CultureInfo"].ToString());%>
    <div class="title-mainright">
	    <h2 class="text1">Giỏ hàng</h2>
    </div>
    
	<div class="content">
		<div class="top-box2"><img src='<%=Url.Content("~/Content/images/top-box2.jpg")%>'/> </div>		
	    <div class="center-box2">
	        <div class="agents-left">
                <div class="titleagents">
                    <span class="title_in">Giỏ hàng của bạn có <%= Session["CountCart"] %> sản phẩm</span>
                    <div class="iconpng7">
                        <img src="<%=Url.Content("~/Content/images/icon20.gif")%>" alt="Download">
                    </div>
                    <span class="linkprint"></span>
                </div>
            <div class="bgabout">
                <div class="content_tab1">
                    <div class="content2">
                        <div class="tbdownload">
                            <table width="500" border="0" cellspacing="0" cellpadding="0">
                                <tbody>
                                    <tr class="top">
                                        <td align="left" style="height: 30px; font-weight: bold; padding-right: 5px;">
                                             <%= ResourceUtil.Instance.GetString("MaSP", ci)%> </td>
                                        <td align="left" style="height: 30px; font-weight: bold; padding-right: 5px;">
                                             <%= ResourceUtil.Instance.GetString("TenSP", ci)%> </td>
                                        <td align="center" style="height: 30px; font-weight: bold; padding-right: 5px;">
                                            <%= ResourceUtil.Instance.GetString("DonGia", ci)%></td>
                                        <td align="center" style="height: 30px; font-weight: bold; padding-right: 5px;">
                                            <%= ResourceUtil.Instance.GetString("SoLuong", ci)%></td>
                                        <td align="center" style="height: 30px; font-weight: bold; padding-right: 5px;">
                                            <%= ResourceUtil.Instance.GetString("ThanhTien", ci)%></td>
                                        <td align="center" style="height: 30px; font-weight: bold; padding-right: 5px;"></td>
                                        <td align="center" style="height: 30px; font-weight: bold; padding-right: 5px;"></td>
                                    </tr>
                                    
                                    <%foreach (var item in Model)
                                      { %>
                                      <tr class="clm">
                                        <td style="color: #000000;">
                                            <%= item.ProductID %>
                                        </td>
                                        
                                        <td style="color: #000000;">
                                            <%= item.ProductName %>
                                        </td>
                                        
                                        <td width="63px" align="center" style="padding-left: 3px;">
                                            <% decimal price= item.ProductPrice?? 0 ; %>
                                            <%= price.ToString("##,#", new CultureInfo("vi-Vn"))%>
                                        </td>
                                        
                                        <td align="right" style="color: #000000;" id="tdCount<%= item.ProductID %>">
                                            <%= item.Count %>
                                        </td>
                                        
                                        <td align="right"  style="color: #000000;">
                                            <%= item.Cost.ToString("##,#", new CultureInfo("vi-Vn"))%>
                                        </td>
                                        
                                         <td style="color: #000000;">
                                            <input id="btnEdit<%= item.ProductID %>" type="button" value="Sửa" onclick="edit(<%= item.ProductID %>)" />
                                        </td>
                                        
                                         <td style="color: #000000;">
                                            <input id="btnDelete<%= item.ProductID %>" type="button" value="Xóa" onclick="deleteProduct(<%= item.ProductID %>)" />
                                        </td>
                                    </tr>
                                    <%} %>
                                </tbody>
                            </table>
                            
                            <br />
                            <br />
                            <div style="text-align:center">
                                Tổng tiền thanh toán : 
                                <b><%= ((decimal)ViewData["Total"]).ToString("##,#", new CultureInfo("vi-Vn"))%></b>
                            </div>
                        </div>
                    </div>
                    <div class="linkbt">
                        &nbsp;
                    </div>
                </div>
            </div>
        </div>
        
        <br />
        <div style="text-align:center;">
            <input type="button" value="Hoàn tất" onclick="submit()" class="button" />
        </div>
        </div>            	
	</div> 

</asp:Content>
